<template>
  <!-- <a-button @click="test">批量设置测试</a-button>
  <a-button @click="test2">批量设置测试2</a-button> -->
  <div
    class="index-table"
    v-for="item in templateData?.personnelAssessmentOptionDimensionality"
    :key="item.type"
  >
    <div class="index-card" v-if="item.type === 1">
      <ConditionBox>
        <template #left>
          <div class="title">{{ item.name }}</div>
          维度权重：<a-tag color="arcoblue">{{ item.weight }}%</a-tag>
        </template>
        <template #right>
          <a-space v-if="props.args.showAdd">
            <a-button type="text" @click="selectKeyPerformanceTemplate">
              引用指标库
            </a-button>
          </a-space>
        </template>
      </ConditionBox>
      <a-divider />
      <IndexTable
        v-model:data="templateData.personnelKeyPerformanceIndexLibrary"
        :upperLimitScore="templateData.upperLimitScore"
        :args="props.args"
        :showRowSelection="props.showRowSelection"
        :onlyShowSelected="onlyShowSelected"
        v-model:returnData="templateData.personnelKeyPerformanceIndexLibraryUse"
        :returnTemp="returnTemp"
        :templateData="templateData"
        v-model:deletedIds="modelDeleted.keyPerformanceIds"
        v-model:selectedIds="modelSelected.keyPerformanceIds"
        v-model:deletedScorerIds="modelDeletedScorer.keyPerformanceIds"
      />
    </div>
    <div class="index-card" v-if="item.type === 2">
      <ConditionBox>
        <template #left>
          <div class="title">{{ item.name }}</div>
          维度权重：<a-tag color="arcoblue">{{ item.weight }}%</a-tag>
        </template>
        <template #right>
          <a-space v-if="props.args.showAdd">
            <a-button type="text" @click="selectAblityIndexTemplate">
              引用指标库
            </a-button>
          </a-space>
        </template>
      </ConditionBox>
      <a-divider />
      <AbilityIndeTable
        v-model:data="templateData.personnelAbilityQualityIndexLibrary"
        :review="props.review"
        :upperLimitScore="templateData.upperLimitScore"
        :showRowSelection="props.showRowSelection"
        v-model:returnData="templateData.personnelAbilityQualityIndexLibraryUse"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="props.args"
        :templateData="templateData"
        v-model:deletedIds="modelDeleted.ablityIds"
        v-model:selectedIds="modelSelected.ablityIds"
        v-model:deletedScorerIds="modelDeletedScorer.ablityIds"
      />
    </div>
    <div class="index-card" v-if="item.type === 3">
      <ConditionBox>
        <template #left>
          <div class="title">{{ item.name }}</div>
          维度权重：<a-tag color="arcoblue">{{ item.weight }}%</a-tag>
        </template>
        <template #right></template>
      </ConditionBox>
      <a-divider />
      <WorkPlanTable
        v-model:data="templateData.personnelWorkPlanIndexLibrary"
        :upperLimitScore="templateData.upperLimitScore"
        :showRowSelection="props.showRowSelection"
        v-model:returnData="templateData.personnelWorkPlanIndexLibraryUse"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="props.args"
        :templateData="templateData"
        v-model:deletedIds="modelDeleted.workPlanIds"
        v-model:selectedIds="modelSelected.workPlanIds"
        v-model:deletedScorerIds="modelDeletedScorer.workPlanIds"
      />
    </div>
    <div class="index-card" v-if="item.type === 4">
      <ConditionBox>
        <template #left>
          <div class="title">{{ item.name }}</div>
          维度权重：<a-tag color="arcoblue">{{ item.weight }}%</a-tag>
        </template>
        <template #right></template>
      </ConditionBox>
      <a-divider />
      <TargetTable
        v-model:data="templateData.personnelTargetIndexLibrary"
        :upperLimitScore="templateData.upperLimitScore"
        :review="props.review"
        :showRowSelection="props.showRowSelection"
        v-model:returnData="templateData.personnelTargetIndexLibraryUse"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="props.args"
        :templateData="templateData"
        v-model:deletedIds="modelDeleted.targetIds"
        v-model:deletedResultIds="modelDeleted.resultIds"
        v-model:selectedTargetIds="modelSelected.targetIds"
        v-model:deletedScorerIds="modelDeletedScorer.targetIds"
      />
    </div>
    <div class="index-card" v-if="item.type === 5">
      <ConditionBox>
        <template #left>
          <div class="title">{{ item.name }}</div>
          <!-- 维度权重：<a-tag color="arcoblue">{{ item.weight }}%</a-tag> -->
        </template>
        <template #right>
          <a-space v-if="props.args.showAdd">
            <a-button type="text" @click="selectAddSubtractTemplate">
              引用指标库
            </a-button>
          </a-space>
        </template>
      </ConditionBox>
      <a-divider />
      <AddSubtractTable
        v-model:data="templateData.personnelAddSubtractIndexLibrary"
        :upperLimitScore="templateData.upperLimitScore"
        :review="props.review"
        :showRowSelection="props.showRowSelection"
        v-model:returnData="templateData.personnelAddSubtractIndexLibraryUse"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="props.args"
        :templateData="templateData"
        v-model:deletedIds="modelDeleted.addSubtractIds"
        v-model:selectedIds="modelSelected.addSubtractIds"
        v-model:deletedScorerIds="modelDeletedScorer.addSubtractIds"
      />
    </div>
    <div class="index-card" v-if="item.type === 6">
      <ConditionBox>
        <template #left>
          <div class="title">{{ item.name }}</div>
          <!-- 维度权重：<a-tag color="arcoblue">{{ item.weight }}%</a-tag> -->
        </template>
        <template #right>
          <a-space v-if="props.args.showAdd">
            <a-button type="text" @click="selectVoteTemplate">
              引用指标库
            </a-button>
          </a-space>
        </template>
      </ConditionBox>
      <!-- <a-alert type="warning" closable>
        <template #title> 请注意！ </template>
        “否决指标”通常指在评估或考核过程中使用的一种特殊标准或维度。它是一种严格的标准，如果在评估中未达到或未满足，则可能对整体表现或成果产生否定的影响且结果为反向操作。
        （1）选项否决：只有有一项是达标，整个考核结果不达标，反之是不达标，整个考核结果达标，不参与计算。
        （2）输入扣分：类似减分项，考核结果总分减去扣分。
      </a-alert> -->
      <a-divider />
      <VetoTable
        v-model:data="templateData.personnelVetoIndexLibrary"
        :review="props.review"
        :upperLimitScore="templateData.upperLimitScore"
        :showRowSelection="props.showRowSelection"
        v-model:returnData="templateData.personnelAddSubtractIndexLibraryUse"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="props.args"
        :templateData="templateData"
        v-model:deletedIds="modelDeleted.vetoIds"
        v-model:selectedIds="modelSelected.vetoIds"
        v-model:deletedScorerIds="modelDeletedScorer.vetoIds"
      />
    </div>
  </div>
  <ScoreHumanModal
    v-model:visible="humanVisible"
    v-model:data="currHumanData"
    @save="saveHUman"
    :type="1"
    :index-type="5"
  />
  <!-- 引用指标库 -->
  <AddVoteTemplateModal
    v-model:visible="voteTemplateVisible"
    @add-index="addVoteIndex"
  />
  <AddSubtractTemplateModal
    v-model:visible="addSubtractTemplateVisible"
    @add-index="addAddSubtractIndex"
  />
  <AblityIndexTemplateModal
    v-model:visible="ablityIndexTemplateVisible"
    @add-index="addAblityIndexIndex"
  />
  <IndexTemplateModal
    v-model:visible="keyPerformanceTemplateVisible"
    @add-index="addKeyPerformanceIndex"
  />
</template>

<script setup lang="ts">
import { cloneDeep } from 'lodash';
import { PerformanceAppraisal } from '@/apis/management/performance/baseSetting/template/type';
import IndexTable from './Tool/AssessmentTable/index.vue';
import AbilityIndeTable from './Tool/AbilityIndeTable/index.vue';
import WorkPlanTable from './Tool/WorkPlanTable/index.vue';
import TargetTable from './Tool/TargetTable/index.vue';
import AddSubtractTable from './Tool/AddSubtractTable/index.vue';
import VetoTable from './Tool/VetoTable/index.vue';
import type {
  ProbationExamine,
  ReturnIndex
} from '@/apis/human/base/variationManager/probation/type';
import { PerformanceAssessmentTemplateDimensionality } from '@/apis/management/performance/baseSetting/template/type';
import { useVModel } from '@vueuse/core';
import ScoreHumanModal from '../conponents/Tool/AbilityIndeTable/ScoreHumanModal.vue';
import AddVoteTemplateModal from '@/views/Management/Performance/BaseSetting/AssessmentTemplate/PerformanceTemplate/compenents/VetoTable/AddVoteTemplateModal.vue';
import AddSubtractTemplateModal from '@/views/Management/Performance/BaseSetting/AssessmentTemplate/PerformanceTemplate/compenents/AddSubtractTable/AddSubtractTemplateModal.vue';
import AblityIndexTemplateModal from '@/views/Management/Performance/BaseSetting/AssessmentTemplate/PerformanceTemplate/compenents/AbilityIndeTable/AblityIndexTemplateModal.vue';
import IndexTemplateModal from '@/views/Management/Performance/BaseSetting/AssessmentTemplate/PerformanceTemplate/compenents/AssessmentTable/IndexTemplateModal.vue';

const props = withDefaults(
  defineProps<{
    data: ProbationExamine;
    /**
     * 0：默认，显示操作
     * 1：显示操作项（适用考核设置时），2：不显示操作项（适用预览时），3：不显示操作项，评分人显示格式不同（适用我的考核）
     */
    review?: number;
    showRowSelection?: boolean;
    onlyShowSelected: boolean;
    returnTemp: number;
    args?: {
      showAdd?: boolean; //显示新增
      showOperation?: boolean; //显示操作
      scorerType?: number; //评分人显示样式。0集成，1区分
      showScoreInput?: boolean;
      showReason?: boolean;
      showIndexScore?: boolean;
      showAppeal?: boolean;
      showAppealHandel?: boolean;
      showIsPass?: boolean;
      showWeight?: boolean;
    };
    selected?: any;
    deleted?: any;
    deletedScorer?: any;
  }>(),
  {
    data: () => ({}),
    args: () => ({
      showAdd: true,
      showOperation: true,
      scorerType: 0,
      showScoreInput: false,
      showReason: false,
      showAppeal: false,
      showAppealHandel: false,
      showIndexScore: false,
      showIsPass: false,
      showRowSelection: false,
      showWeight: true
    }),
    selected: () => [],
    deleted: () => [],
    deletedScorer: () => []
  }
);
const emit = defineEmits([
  'handleOk',
  'update:data',
  'update:returnIndex',
  'update:selected',
  'update:deleted',
  'update:deletedScorer'
]);
const templateData = useVModel(props, 'data', emit);
const modelSelected = useVModel(props, 'selected', emit);
const modelDeleted = useVModel(props, 'deleted', emit);
const modelDeletedScorer = useVModel(props, 'deletedScorer', emit);
const humanVisible = ref(false);
const currHumanData = ref<any>({ scorer: [] });
const saveHUman = () => {
  templateData.value?.personnelVetoIndexLibrary?.forEach((item) => {
    item.scorer = currHumanData.value.scorer;
  });
  templateData.value?.personnelAddSubtractIndexLibrary?.forEach((item) => {
    item.scorer = currHumanData.value.scorer;
  });
  humanVisible.value = false;
};
const voteTemplateVisible = ref(false);
const selectVoteTemplate = () => {
  voteTemplateVisible.value = true;
};
const addVoteIndex = (data: any) => {
  templateData.value.personnelVetoIndexLibrary = [
    ...templateData.value.personnelVetoIndexLibrary,
    ...data
  ];
};
const addSubtractTemplateVisible = ref(false);
const selectAddSubtractTemplate = () => {
  addSubtractTemplateVisible.value = true;
};
const addAddSubtractIndex = (data: any) => {
  templateData.value.personnelAddSubtractIndexLibrary = [
    ...templateData.value.personnelAddSubtractIndexLibrary,
    ...data
  ];
};
const keyPerformanceTemplateVisible = ref(false);
const selectKeyPerformanceTemplate = () => {
  keyPerformanceTemplateVisible.value = true;
};
const addKeyPerformanceIndex = (data: any) => {
  templateData.value.personnelKeyPerformanceIndexLibrary = [
    ...templateData.value.personnelKeyPerformanceIndexLibrary,
    ...data
  ];
};
const ablityIndexTemplateVisible = ref(false);
const selectAblityIndexTemplate = () => {
  ablityIndexTemplateVisible.value = true;
};
const addAblityIndexIndex = (data: any) => {
  data.forEach((item) => {
    item.personnelAbilityQualityIndexLibraryScoringStandard =
      item.performanceAbilityQualityIndexLibraryScoringStandard;
  });

  templateData.value.personnelAbilityQualityIndexLibrary = [
    ...templateData.value.personnelAbilityQualityIndexLibrary,
    ...data
  ];
};
const test = () => {
  let user = [
    {
      userName: '张三',
      userId: 105,
      isWaitDone: 2,
      weight: 100,
      type: 1
    }
  ];
  templateData.value.personnelKeyPerformanceIndexLibrary?.forEach((item) => {
    item.scorer = user;
  });
  templateData.value.personnelAbilityQualityIndexLibrary?.forEach((item) => {
    item.scorerStatus = {
      appoint: { status: 1, weight: 100 },
      junior: { status: 0, weight: 0 },
      other: { status: 0, weight: 0 },
      peer: { status: 0, weight: 0 },
      superiors: { status: 0, weight: 0 }
    };
    item.scorer = user;
  });
  templateData.value.personnelAddSubtractIndexLibrary?.forEach((item) => {
    item.scorer = user;
  });
  templateData.value.personnelVetoIndexLibrary?.forEach((item) => {
    item.scorer = user;
  });
  templateData.value.personnelWorkPlanIndexLibrary?.forEach((item) => {
    item.scorer = user;
  });
  templateData.value.personnelTargetIndexLibrary?.forEach((item) => {
    item?.personnelKeyResultsIndexLibrary?.forEach((result) => {
      result.scorer = user;
    });
  });
};
const test2 = () => {
  templateData.value.personnelKeyPerformanceIndexLibrary?.forEach((item) => {
    item.scorer.forEach((sco) => {
      sco.score = 50;
    });
  });
  templateData.value.personnelAbilityQualityIndexLibrary?.forEach((item) => {
    item.scorerStatus = {
      appoint: { status: 1, weight: 100 },
      junior: { status: 0, weight: 0 },
      other: { status: 0, weight: 0 },
      peer: { status: 0, weight: 0 },
      superiors: { status: 0, weight: 0 }
    };
    item.scorer.forEach((sco) => {
      sco.score = 50;
    });
  });
  templateData.value.personnelAddSubtractIndexLibrary?.forEach((item) => {
    item.scorer.forEach((sco) => {
      sco.score = 50;
    });
  });
  templateData.value.personnelVetoIndexLibrary?.forEach((item) => {
    item.scorer.forEach((sco) => {
      if (sco.scoreType === 1) {
        sco.whetherViolate = false;
      } else {
        sco.score = 50;
      }
    });
  });
  templateData.value.personnelWorkPlanIndexLibrary?.forEach((item) => {
    item.scorer.forEach((sco) => {
      sco.score = 50;
    });
  });
  templateData.value.personnelTargetIndexLibrary?.forEach((item) => {
    item?.personnelKeyResultsIndexLibrary?.forEach((result) => {
      result.scorer.forEach((sco) => {
        sco.score = 50;
      });
    });
  });
};
</script>

<style lang="less" scoped>
.index-table {
  // margin: 20px;
  .index-card {
    margin: 20px 0;
    box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.12);
    padding: 20px;
    border-radius: 2px;
    .title {
      font-weight: 600;
      font-size: 18px;
    }
  }
}
// :deep(.arco-table-th),
// :deep(.arco-table-td) {
//   height: 40px;
// }
:deep(.arco-scrollbar-track-direction-horizontal) {
  bottom: -7px;
}
</style>
